<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告" prop="merchantid">
        <el-input
          v-model="queryParams.merchantid"
          placeholder="请输入广告位置名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出租时间" prop="cztime">
        <el-input
          v-model="queryParams.cztime"
          placeholder="请输入出租时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到期时间" prop="duetime">
        <el-input
          v-model="queryParams.duetime"
          placeholder="请输入到期时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="总费用" prop="cost">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.cost"-->
      <!--          placeholder="请输入总费用"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:advertising:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:advertising:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:advertising:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:advertising:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="advertisingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="aid" width="100px"/>
      <el-table-column label="广告位置" align="center" prop="merchantid" />
      <el-table-column label="出租时间" align="center" prop="cztime" >
        <template slot-scope="scope">
            <span v-if="scope.row.cztime != null">
            	{{ parseTime(scope.row.cztime,  '{y}年{m}月{d}日 {h}:{i}:{s} 周{a}') }}
            </span>
        </template>
      </el-table-column>
      <el-table-column label="到期时间" align="center" prop="duetime" >
        <template slot-scope="scope">
            <span v-if="scope.row.duetime != null">
            	{{ parseTime(scope.row.duetime,  '{y}年{m}月{d}日 {h}:{i}:{s} 周{a}') }}
            </span>
        </template>
      </el-table-column>
      <el-table-column label="是否缴费" align="center" prop="cost" :formatter="fmgender" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:advertising:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:advertising:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handlelist(scope.row)"
            v-hasPermi="['system:advertising:edit']"
          >缴费</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改广告费对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="广告" prop="merchantid">
          <el-input v-model="form.merchantid" placeholder="广告位置名" />
        </el-form-item>
        <el-form-item label="出租时间" prop="cztime">
          <!--          <el-input v-model="form.cztime" placeholder="请输入出租时间" />-->
          <el-date-picker v-model="form.cztime" format="yyyy-MM-dd  HH:mm:ss"  type="date"
                          value-format="timestamp" placeholder="请输入出租时间" />
        </el-form-item>
        <el-form-item label="到期时间" prop="duetime">
          <!--          <el-input v-model="form.duetime" placeholder="请输入到期时间" />-->
          <el-date-picker v-model="form.duetime" format="yyyy-MM-dd  HH:mm:ss"  type="date"
                          value-format="timestamp" placeholder="请输入到期时间" />
        </el-form-item>
        <el-form-item label="总费用" prop="cost">
<!--          <el-input v-model="form.cost" placeholder="请输入总费用" />-->
          <el-select  v-model="form.cost" placeholder="请选择是否缴费">
            <el-option label="未缴费" value="1"></el-option>
            <el-option label="已缴费" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--    缴费-->
    <el-dialog :title="title" :visible.sync="open2" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单编号:">
          <el-input type="text"  v-model="outTradeNo" />
        </el-form-item>
        <el-form-item label="支付业务:">
          <el-input type="text" v-model="ruleForm.order_detail" />
        </el-form-item>
        <el-form-item label="价格套餐:">
          <!--          <el-input type="text" style="width: 200px" v-model="ruleForm.order_price" />-->
          <el-select  v-model="ruleForm.order_price" placeholder="请选择活动套餐">
            <el-option label="¥80 / 月" value="80"></el-option>
            <el-option label="¥30 / 季度" value="230"></el-option>
            <el-option  label="¥960 / 年" value="960"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注说明:">
          <el-input type="textarea" placeholder="备注"  v-model="ruleForm.goods_info" />
        </el-form-item>



        <div  style="text-align:right;">
          <el-button type="primary" round @click="submit" >提交订单</el-button>
        </div>

      </el-form>


    </el-dialog>
  </div>
</template>

<script>
import { listAdvertising, getAdvertising, delAdvertising, addAdvertising, updateAdvertising, getDateNums } from "@/api/system/advertising";
import axios from 'axios'
import {getProperty} from "@/api/system/property";
export default {
  name: "Advertising",
  data() {
    return {
      isSign: false,
      signSrc: '',
      clientHeight: '',
      uid: null,
      outTradeNo:'',

      ruleForm:{
        // order_number:'20025715247523',
        order_number:'',
        order_detail:'广告费',
        order_price:'80',
        goods_info:'',
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 广告费表格数据
      advertisingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchantid: null,
        cztime: null,
        duetime: null,
        cost: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantid: [
          { required: true, message: "广告位置不能为空", trigger: "blur" }
        ],
        cztime: [
          { required: true, message: "出租时间不能为空", trigger: "blur" }
        ],
        duetime: [
          { required: true, message: "到期时间不能为空", trigger: "blur" }
        ],
        cost: [
          { required: true, message: "总费用不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.outTradeNo= this.order_nums().outTradeNo
  },
  methods: {
    fmgender(row,column){
      return row.cost == '1' ? '未缴费' : '已缴费'
    },
    //第一种简单的 生成随机订单号，时间+日期，格式为:20200313123456，这种数据量大的时候有可能会重复
    order_nums() {
      var that = this;
      var outTradeNo = ""; //订单号
      for (var i = 0; i < 6; i++) //6位随机数，用以加在时间戳后面。
      {
        outTradeNo += Math.floor(Math.random() * 10);
      }

      outTradeNo = String(getDateNums(new Date())) + String(outTradeNo)
      that.outTradeNo = outTradeNo;

    },

    submit() {
      axios.post(
        "http://114.132.152.75:8088/order/alipay?outTradeNo=" +
        this.outTradeNo +
        "&subject=" +
        this.ruleForm.order_detail +
        "&totalAmount=" +
        this.ruleForm.order_price +
        "&body=" +
        this.ruleForm.goods_info
      ).then((res) => {
        const newTab = window.open();
        // 添加之前先删除一下，如果单页面，页面不刷新，添加进去的内容会一直保留在页面中，二次调用form
        const divForm = document.getElementsByTagName("div");
        if (divForm.length) {
          document.body.removeChild(divForm[0]);
        }
        const div = document.createElement("div");
        div.innerHTML = res.data; // data就是接口返回的form 表单字符串
        document.body.appendChild(div);
        // document.forms[0].setAttribute("target", "_blank"); // 新开窗口跳转
        // document.forms[0].submit();

        newTab.document.body.appendChild(div);

        newTab.document.forms[0].submit();
      })
    },
    /** 查询广告费列表 */
    getList() {
      this.loading = true;
      listAdvertising(this.queryParams).then(response => {
        this.advertisingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        aid: null,
        merchantid: null,
        cztime: null,
        duetime: null,
        cost: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.aid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加广告费";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const aid = row.aid || this.ids
      getAdvertising(aid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改广告费";
      });
    },
    handlelist(row){
      this.reset();
      const aid = row.aid || this.ids
      getAdvertising(aid).then(response => {
        this.form = response.data;
        this.open2 = true;
        this.title = "缴费";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.aid != null) {
            updateAdvertising(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAdvertising(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const aids = row.aid || this.ids;
      this.$modal.confirm('是否确认删除广告费编号为"' + aids + '"的数据项？').then(function() {
        return delAdvertising(aids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/advertising/export', {
        ...this.queryParams
      }, `advertising_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
